package im.clusterhead;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import im.eventservice.*;
import im.general.Config;
import jade.core.behaviours.CyclicBehaviour;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Vector;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
//import java.util.logging.SimpleFormatter;

/**
 *
 * @author Giovanni
 */
public class ClusterHeadLogBehaviour extends CyclicBehaviour {

    private Logger log = Logger.getLogger("Cluster Head");
    private FileHandler fh;
    private OurFormatter sf = new OurFormatter();
    private ClusterHeadAgent ch;

    public ClusterHeadLogBehaviour(ClusterHeadAgent ch){
        this.ch = ch;
        try {
            fh = new FileHandler(Config.LOGGING_DIRECTORY + "//" + Config.CLUSTER_HEAD_LOG_FILE + ch.getLocalName() + ".txt");
        } catch (IOException ex) {
            Logger.getLogger(ClusterHeadLogBehaviour.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(ClusterHeadLogBehaviour.class.getName()).log(Level.SEVERE, null, ex);
        }
        fh.setFormatter(sf);
        log.addHandler(fh);
        log.setLevel(Level.ALL);
    }

    public int attachedNodeCounter() {

        //es.heads tutti i cluster heads nella rete
        Vector chs = ch.getNodesList();


        return chs.size();

    }

    public String getTimeStampNow() {

        Calendar cal = new GregorianCalendar();
        int giorno = cal.get(Calendar.DAY_OF_MONTH);
        int mese = cal.get(Calendar.MONTH);
        int anno = cal.get(Calendar.YEAR);
        int ora = cal.get(Calendar.HOUR);
        int minuti = cal.get(Calendar.MINUTE);
        int secondi = cal.get(Calendar.SECOND);
        String orario;

        if (cal.get(Calendar.AM_PM) == 0) {
            orario = "A.M.";
        } else {
            orario = "P.M.";
        }

        return "" + giorno + "-" + (mese + 1) + "-" + anno + " " + ora + ":" + minuti + ":" + secondi + " " + orario + "";

    }

    /*public double getStatus(){

    return ch.getStatus().getStatusIndex();
    }*/
    public double getBWK(){

        return ch.getStatus().getFreeBWKbps();
    }

    @Override
    public void action() {
        //throw new UnsupportedOperationException("Not supported yet.");

        //mettiamo il log
        //stampiamo il numero dei cluster head etc etc...
        int attachedNodeCounter = attachedNodeCounter();

        if (ch.getStatus() == null) {
            
            block(1000);
        } else {

            if (attachedNodeCounter == 0) {
                log.log(Level.ALL, "" + getTimeStampNow() + ";" + attachedNodeCounter + ";" + ch.getStatus().getStatusIndex() + ";" + ch.getStatus().isPlugged() + ";" + ch.getStatus().getChargeReminderMah() + ";" + ch.getStatus().getFreeBWKbps() + ";" + ch.getStatus().getFreeCPUMhz() + ";" + ch.getStatus().getFreeDiskMB() + ";" + ch.getStatus().getFreeRamMB() + ";" + "INF" + ";" + "INF" + ";");
            } else {
                 double statOnAttach = ch.getStatus().getStatusIndex() / attachedNodeCounter;
                 double bandOnAttach = getBWK() / attachedNodeCounter;

                log.log(Level.ALL, "" + getTimeStampNow() + ";" + attachedNodeCounter + ";" + ch.getStatus().getStatusIndex() + ";" + ch.getStatus().isPlugged() + ";" + ch.getStatus().getChargeReminderMah() + ";" + ch.getStatus().getFreeBWKbps() + ";" + ch.getStatus().getFreeCPUMhz() + ";" + ch.getStatus().getFreeDiskMB() + ";" + ch.getStatus().getFreeRamMB() + ";" + statOnAttach + ";" + bandOnAttach + ";");

            }

            block(5000);

        }

    }
}
